package com.example.teamtribe.common.send;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.Application;
import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.database.Request;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

public class SendActivity extends BaseActivity implements View.OnClickListener, SendContract.View {

    public static final int REQUEST_REQU = 1;
    public static final int REQUEST_RESP = 2;
    public static final int RESULT_OK = -1;

    private EditText etReason;
    private SendContract.Presenter presenter;
    private Request request;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);

        etReason = findViewById(R.id.et_reason);
        presenter = new SendPresenter(this);

        Intent intent = getIntent();
        request = intent.getParcelableExtra("request");
        user = intent.getParcelableExtra("user");

        findViewById(R.id.bt_send).setOnClickListener(this);

        TextView title = findViewById(R.id.tv_title);
        if (request == null) {
            title.setText("好友请求");
        } else {
            title.setText("回复");
        }
        setInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_send:
                String reason = etReason.getText().toString().trim();
                if (request == null) {
                    presenter.request(user.getRealId(), reason);
                } else {
                    presenter.response(request.getRealId(), 2, reason);
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void next() {
        Application.getBinder().getResponseMeg();
        if (request != null) {
            request.setResult(2);
            request.setResponseReason(etReason.getText().toString().trim());
            request.saveOrUpdate();
        }
        Intent intent = new Intent();
        intent.putExtra("request", request);
        setResult(RESULT_OK, intent);
        finish();
    }

    private void setInfo() {
        ImageView ivHead = findViewById(R.id.iv_head);
        TextView tvNickname = findViewById(R.id.tv_nickname);
        TextView tvGender = findViewById(R.id.tv_info);
        Glide.with(this).load(Utility.getUri(user.getPortrait())).into(ivHead);
        tvNickname.setText(user.getNickname());
        tvGender.setText(user.getGenderString());
    }
}
